package Parsers::Sequest;

use strict;
use warnings;

my $VERSION = '1.00';
our @EXPORT_OK = qw(analyze);
use base qw(Exporter);


sub analyze{
	my @sequest_files = @_;
	my %info_sequest;

	#Parseo todos los xls de sequest que se van a juntar
	foreach(@sequest_files){
		my %parsed_sequest = parse($_);
		foreach(keys %parsed_sequest){
			$info_sequest{$_} = $parsed_sequest{$_};
		}
	}
	return %info_sequest;
}

sub parse{
	my $filename = $_[0];
	my %sequest;
	my $error = open my $IN, '<', $filename;
	my $first_line = 0;
	while(<$IN>){
		chomp;
		if($first_line == 0){
			$first_line = 1;
			next;
		}

		my ($file, $scan1, $scan2, $ms, $reference, $actual_mass, $deltamass, $z, $peptide,
			$prob, $xcorr, $deltacn, $sp, $rsp, $ions, $count, $D) = split /\t/, $_;

		my $peptide_wo_mods = $peptide;       # $peptide  ---> QFAPEY(21)EKIANILK
		$peptide_wo_mods=~s/\(.{1,4}\)//g;

		$file = $file . '.' . $scan1 . '.' . $scan2;
		$sequest{$file}{'ms'} = $ms;
		$sequest{$file}{'unmodified_sequence'} = $peptide_wo_mods;
		$sequest{$file}{'modified_sequence'} = $peptide;
		$sequest{$file}{'z'} = $z;
		$sequest{$file}{'actual_mass'} = $actual_mass;
		$sequest{$file}{'deltamass'} = $deltamass;
		$sequest{$file}{'xcorr'} = $xcorr;
		$sequest{$file}{'deltacn'} = $deltacn;
		$sequest{$file}{'sp'} = $sp;
		$sequest{$file}{'rsp'} = $rsp;
		$sequest{$file}{'ions'} = $ions;
		$sequest{$file}{'d'} = $D;
		$sequest{$file}{'db_first_match'} = (split /\|/, $reference)[1];   # $reference --> sp|P13667|PDIA4_HUMAN
		$sequest{$file}{'count'} = $count;
	}
	close $IN;
	return %sequest;
}

1;  #63


__END__

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 LIMITATIONS

=head1 SEE ALSO

=cut
